package com.example.mediamoudle.pickvideo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mediamoudle.R;
import com.example.mediamoudle.utlis.Util;

import java.util.ArrayList;

/**
 * 本地选择视频adapter
 *
 * @auth sxy
 * @date 2022/10/21 11:35
 */
public class PickSelectVideoAdapter extends RecyclerView.Adapter<PickSelectVideoAdapter.ViewHolder> {

    private String TAG = PagerAdapter.class.getSimpleName();
    private Context mContext;
    private ArrayList<VideoFile> videoFileList;

    public PickSelectVideoAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(ArrayList<VideoFile> videoFileList) {
        this.videoFileList = videoFileList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shortvideo_video_picker_select_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(mContext)
                .load(videoFileList.get(position).getPath())
                .apply(new RequestOptions().transform(
                       new RoundedCornersTransform(
                                dp2px(2),true,true,true,true)
                ))
                .into(holder.ivShow);

        holder.tvDuration.setText(Util.getDurationString(videoFileList.get(position).getDuration()));
        holder.ivDelete.setTag(position);
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               int pos = (int) v.getTag();
                if (itemSelectDeleteListener != null) {
                    itemSelectDeleteListener.deleteItemSelect(pos);
                }
            }
        });
        holder.itemView.setOnClickListener(v->{
            Log.i("TAG", "VideoPreview: videoFile22->:"+videoFileList.get(position).getDuration()+",->position:"+position);
            itemSelectDeleteListener.VideoPreview(videoFileList.get(position),videoFileList,position);
        });

    }

    @Override
    public int getItemCount() {
        return videoFileList == null ? 0 : videoFileList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivShow;
        ImageView ivDelete;
        TextView tvDuration;

        public ViewHolder(View itemView) {
            super(itemView);
            ivShow = itemView.findViewById(R.id.iv_show);
            ivDelete = itemView.findViewById(R.id.iv_delete);
            tvDuration = itemView.findViewById(R.id.tv_duration_pick);

        }

    }

    public interface ItemSelectDeleteListener {
        void deleteItemSelect(int pos);
        void VideoPreview(VideoFile videoFile, ArrayList<VideoFile> mList, int position);
    }

    private ItemSelectDeleteListener itemSelectDeleteListener;

    public void setItemSelectListener(ItemSelectDeleteListener itemSelectDeleteListener) {
        this.itemSelectDeleteListener = itemSelectDeleteListener;
    }
    public int dp2px(int dp){
        return (int) (mContext.getResources().getDisplayMetrics().density*dp+0.5);
    }
}
